<include file="../Public/_header.html" />
<dl style="margin:10px 10px 5px; height:22px" class="clear">
  <dt class="left alR " style="width:43px;">标示：</dt>
  	<dd class="left alL"><input type="text" id="sort_code" value="{$rule['sort_code']}">&nbsp;填英文</dd>
  <br><br>
	<dt class="left alR " style="width:43px;">规则：</dt>
	<dd class="left alL">
		<input type="checkbox" id="checkrule" value="USETYPE">用户类型
		&nbsp;&nbsp;&nbsp;&nbsp;
		<input type="checkbox" id="checkrule" value="AMOUNT">最低额度
	</dd>
	<br/><br/>
  	<dt class="left alR " style="width:43px;">&nbsp;&nbsp;</dt>
  	<dd class="left alL">
  		<input type="checkbox" id="checkrule" value="PARTNERID">指定商户
  		&nbsp;&nbsp;&nbsp;&nbsp;
		<input type="checkbox" id="checkrule" value="GOODSID">指定商品
  	</dd>
  	<br/><br/>
  	<dt class="left alR " style="width:43px;">说明：</dt>
  	<dd class="left alL"><input type="text" id="_desc" value="{$rule['desc']}">&nbsp;</dd>
  	<br><br>
</dl>

<div class="clear"></div>
<br>
<div style="background:#F8F8F8; border-top:1px solid #E6E6E6; padding:5px;" class="alR">
  <php>if (isset($rule)) {</php>
  <input type="button" class="btn_b" onclick="_edit()" value="提交" />
  <php>}else {</php>
  <input type="button" class="btn_b" onclick="_add()" value="提交" />
  <php>}</php>
  <input type="button" class="btn_w" onclick="ui.box.close();" value="取消" />
</div>
<div id="showerror"></div>
<script type="text/javascript">

//添加地区
function _add() {
	var desc = $('#sort_code').val();
	if(desc=='') {
		ui.error('请输入标示');
		return ;
	}

	var checkrule = _getruledata();
	if(checkrule=='') {
		ui.error('请选择规则');
		return ;
	}
	
	$.ajax({
 			type: "POST",
 			url: "{:U('/Cashcoupon/doAddRule')}",
 			data:   {sort_code:desc,checkrule:checkrule,desc:$('#_desc').val()},
 			success: function(msg){
 										$('#showerror').html(msg);
 										var obj	=	eval('(' + msg + ')');
								    if(obj.status==1){
												var url = "{:U('/Cashcoupon/rule')}";
												k_success(obj.msg);
												k_page_redirect(url);
										}else{
												alert(obj.msg);
										}
 								}
		});
}


function _getruledata(){
	var ret = "";
	$("input[type='checkbox'][id='checkrule']").each(function(){
		   if($(this).attr("checked")==true){
			   if(ret == "")ret += $(this).attr("value");
			   else ret += ","+$(this).attr("value");
		   }
		});
	alert(ret);
	return ret;
} 

//编辑地区
function _edit() {
	var id   = '{$rule['id']}';

	if(desc=='') {
		ui.error('请输入标示');
		return ;
	}

	var checkrule = _getruledata();
	if(checkrule=='') {
		ui.error('请选择规则');
		return ;
	}
	
	$.ajax({
 			type: "POST",
 			url: "{:U('/Cashcoupon/doEditRule')}",
 			data:   {id:id,sort_code:desc,checkrule:checkrule,desc:$('#_desc').val()},
 			success: function(msg){
 										$('#showerror').html(msg);
 										var obj	=	eval('(' + msg + ')');
								    if(obj.status==1){
												var url = "{:U('/Cashcoupon/rule')}";
												k_success(obj.msg);
												k_page_redirect(url);
										}else{
												alert(obj.msg);
										}
 								}
		});
}

</script>
